// synchronized给类加锁，以及线程安全练习

import java.util.Random;

// 死锁演示：最先线程1拿到钱，线程2拿到或，但是交换时谁都不肯先给对方，所以就死锁了
public class Main {
    public static void main(String[] args) {
        Object qian = new Object();
        Object huo = new Object();
        Thread t1 = new Thread(new Runnable() {
            @Override
            public void run() {
                synchronized (qian){
                    // 为了确保线程1先拿到钱
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }

                    synchronized (huo){
                        System.out.println("线程1拿到了钱和或");
                    }
                }
            }
        });

        Thread t2 = new Thread(){
            @Override
            public void run(){
                synchronized (huo){
                    // 为了确保线程2先拿到或
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }

                    synchronized (qian){
                        System.out.println("线程2拿到了钱和或");
                    }
                }
            }
        };

        t1.start();
        t2.start();
        try {
            t1.join();
            t2.join();
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }
}




 // 两个线程实现一个变量自增到10000，并解决线程安全问题
 class Main2 {
     public static int count = 0;

     public static void main(String[] args) {
         Thread t1 = new Thread(){
             @Override
             public void run(){
                 for (int i = 0; i < 5000; i++) {
                     synchronized (Integer.class){
                         count++;
                     }
                 }
             }
         };
         Thread t2 = new Thread(new Runnable() {
             @Override
             public void run() {
                 for (int i = 0; i < 5000; i++) {
                     synchronized (Integer.class){
                         count++;
                     }
                 }
             }
         });
         t1.start();
         t2.start();
         try {
             t1.join();
             t2.join();
         } catch (InterruptedException e) {
             throw new RuntimeException(e);
         }

         System.out.println(count);
     }
 }


 // 使用两个线程1+...100_0000，一个线程自增偶数，另一个增奇数
 class Main1 {
     public static int sum1 = 0;
     public static int sum2 = 0;
     public static int sumAll = 0;

     public static void main(String[] args) {
         int[] a = new int[100_0000];
         Random random = new Random();
         for (int i = 0; i < 100_0000; i++) {
             a[i] = random.nextInt(100);
         }


         Thread t1 = new Thread(()->{
             for (int i = 0; i < 100_0000; i+=2) {
                 sum1+=a[i];
             }
         });
         Thread t2 = new Thread(()->{
             for (int i = 1; i < 100_0000; i+=2) {
                 sum2+=a[i];
             }
         });

         long begin = System.currentTimeMillis();
         t1.start();
         t2.start();
         try {
             t1.join();
             t2.join();
         } catch (InterruptedException e) {
             throw new RuntimeException(e);
         }

         sumAll = sum1 + sum2;

         long end = System.currentTimeMillis();

         System.out.println("和为："+sumAll+" 时间为："+(end-begin));
     }
 }